package com.fox.imagedb.listener;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JSplitPane;

import com.fox.imagedb.gui.MainPanel;
import com.fox.imagedb.gui.TopPanel;

public class MainPanelDividerListener implements PropertyChangeListener {

	private MainPanel mainPanel;

	private int oldWidth;

	private int oldHeight;

	final private int DIVIDER_SIZE = 3;

	public MainPanelDividerListener(MainPanel mainPanel) {
		super();
		this.mainPanel = mainPanel;
		this.oldWidth = mainPanel.getImagePanel().getWidth();
		this.oldHeight = mainPanel.getDividerLocation();
	}

	@Override
	public void propertyChange(PropertyChangeEvent e) {

		if (e.getPropertyName().equals(JSplitPane.DIVIDER_LOCATION_PROPERTY)) {
			int newValue = (Integer) e.getNewValue();
			if (e.getSource() instanceof MainPanel && newValue != oldHeight) {
				oldHeight = newValue - DIVIDER_SIZE;
				mainPanel.getImagePanel().resizeImage(-1, newValue);
			}
			if (e.getSource() instanceof TopPanel && newValue != oldWidth) {
				oldWidth = newValue - DIVIDER_SIZE;
				mainPanel.getImagePanel().resizeImage(newValue, -1);
			}
		}

	}

}
